package com.kis.core.util.http;

import com.kis.core.util.enums.ConstEnum;
import com.kis.core.util.mac.MacUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by wongloong on 16-5-24.
 */
public class HttpUtils {
    @Autowired
    private static Environment env;

    public static boolean sendServerRunInfo() {
        boolean result = false;
        try {
            String param = "?mac=" + MacUtils.getMac();
            URL url = new URL(ConstEnum.REGISSERVER.getValue() + ConstEnum.REGISSERVERPATH.getValue() + param);
//            URL url = new URL(env.getProperty("registerServerIp")+ ConstEnum.REGISSERVERPATH.getValue() + param);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            Map<String, List<String>> map = urlConnection.getHeaderFields();
            // 遍历所有的响应头字段
//            for (String key : map.keySet()) {
//                System.out.println(key + "--->" + map.get(key));
//            }
            result = true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getConst(String key) {
        String result = "";
        if (null != key && !key.equals("")) {
            Properties props = new Properties();
            InputStream in = null;
            try {
                in = HttpUtils.class.getClass().getResourceAsStream("/const.properties");
                props.load(in);
                result = props.getProperty(key);
            } catch (Exception e1) {
                e1.printStackTrace();
            } finally {
                try {
                    if (null != in) {
                        in.close();
                    }
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 获取访问者IP
     * <p>
     * 在一般情况下使用Request.getRemoteAddr()即可，但是经过nginx等反向代理软件后，这个方法会失效。
     * <p>
     * 本方法先从Header中获取X-Real-IP，如果不存在再从X-Forwarded-For获得第一个IP(用,分割)，
     * 如果还不存在则调用Request .getRemoteAddr()。
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip.equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip.equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip.equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }

        if (ip.equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        } else {
            /**
             *
             * 多次反向代理后会有多个IP值，第一个为真实IP。
             */
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        return ip;
    }
}
